﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ECare.BUS.Workers;
using ECare.Client.templates;

using ECare.BUS.Data;
using ECare.BUS;

namespace ECare.Client.frms
{
    public partial class frmCheckInfo : frmInfo
    {
        public frmCheckInfo()
        {
            InitializeComponent();

            this.Load += frmCheckInfo_Load;
        }

        void frmCheckInfo_Load(object sender, EventArgs e)
        {
            this.LoadInformation();
        }

        private void LoadInformation()
        {
            if (this.entity != null)
            {
                var checkInfo = this.entity as SupplyInfo;

                this.ucInInfo1.CurrentSupply = checkInfo;
                this.ucInCheckInfo1.CurrentSupply = checkInfo;


                this.ucInInfo1.isUpdateDate = true;this.ucInInfo1.LoadInformation();
                this.ucInCheckInfo1.LoadInformation();


            }
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            var checkinfo = this.entity as SupplyInfo;

            var originalstatus = (int)checkinfo.supply_checkstatus;

            if (this.rbOK.Checked)
            {
                checkinfo.supply_checkstatus = CheckStatus.Qualitified;
            }
            else if (this.rbFailed.Checked)
            {
                checkinfo.supply_checkstatus = CheckStatus.Rejected;
            }
            else if (this.rbUnfinished.Checked)
            {
                checkinfo.supply_checkstatus = CheckStatus.UnFinished;
            }
            else if (this.rbSpecialAcceptance.Checked)
            {
                checkinfo.supply_checkstatus = CheckStatus.SpecialAcceptance;
            }

            checkinfo.supply_check_remark = this.txtRemark.Text;

            checkinfo.supply_check_by = ApplicationData.CurrentUserInfo.UserId;
            
            int currentstatus = (int)checkinfo.supply_checkstatus;


            checkinfo.supply_invalid_time = this.ucInInfo1.GetInvalidate();
            //
            if (checkinfo.supply_invalid_time <= DateTime.Now && 
                (currentstatus == (int)CheckStatus.Qualitified || currentstatus==(int)CheckStatus.SpecialAcceptance)
                )
            {
                //已经失效了
                MessageBox.Show("物料有效期已过，不能质检通过.", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }



            if (originalstatus != currentstatus)
            {
                //生成一条审批记录。记录状态的改变.
                var recordinfo = new CheckInfo
                {
                    Check_By = ApplicationData.CurrentUserInfo.UserId,
                    Check_Time = DateTime.Now,
                    CheckType = CheckType.Supply,
                    Current_Status = currentstatus,
                    EntityId = checkinfo.supply_id,
                    Original_Status = originalstatus,
                    Remark = this.txtRemark.Text
                };
                CheckRecordsHelper.CreateCheck(recordinfo);
            }









            SupplyInfoHelper.CheckSupply(checkinfo);
            this.Close();
        }

        private void eButton1_Click(object sender, EventArgs e)
        {
            var worker = new InReportBuilder { CurrentSupplyInfo = this.entity as SupplyInfo };
            worker.RunWorkerCompleted += worker_RunWorkerCompleted;
            worker.RunWorkerAsync();
            frmloading = new frmLoading();
            frmloading.ShowDialog();
        }

        void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            frmloading.Close();

            var frmprint = new frmPrint(); frmprint.file = e.Result as string;
            frmprint.ShowDialog();
        }

    }
}
